﻿using System;
using System.IO;
using System.Windows.Forms;

namespace task_1
{
    public partial class MainForm : Form
    {
        private TextBox txtFile1;
        private TextBox txtFile2;
        private string file1Path;
        private string file2Path;

        public MainForm()
        {
            InitializeComponent();
        }

        private void btnSelectFile1_Click(object sender, EventArgs e)
        {
            SelectFile(txtFile1, ref file1Path);
        }

        private void btnSelectFile2_Click(object sender, EventArgs e)
        {
            SelectFile(txtFile2, ref file2Path);
        }

        private void SelectFile(TextBox targetTextBox, ref string filePath)
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    targetTextBox.Text = Path.GetFileName(openFileDialog.FileName);
                    filePath = openFileDialog.FileName;
                }
            }
        }

        private void btnMerge_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(file1Path) || string.IsNullOrWhiteSpace(file2Path))
            {
                MessageBox.Show("请先选择两个文件！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            try
            {
                string content1 = File.ReadAllText(file1Path);
                string content2 = File.ReadAllText(file2Path);
                string combinedContent = content1 + content2;

                string dataDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data");
                Directory.CreateDirectory(dataDirectory);

                string timestamp = DateTime.Now.ToString("yyyyMMddHHmmss");
                string newFilePath = Path.Combine(dataDirectory, $"merged_{timestamp}.txt");
                File.WriteAllText(newFilePath, combinedContent);

                MessageBox.Show($"文件合并成功！\n保存路径：{newFilePath}", "成功",
                              MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (FileNotFoundException ex)
            {
                MessageBox.Show($"文件未找到：{ex.Message}", "错误",
                              MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (UnauthorizedAccessException ex)
            {
                MessageBox.Show($"无权限访问文件：{ex.Message}", "错误",
                              MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (IOException ex)
            {
                MessageBox.Show($"文件读写错误：{ex.Message}", "错误",
                              MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"操作失败：{ex.Message}", "错误",
                              MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {

        }
    }
}
